//---------------------------------------------------------------------------------------//
package 
{

	import com.facebook.Facebook;
	import com.facebook.utils.FacebookSessionUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	import it.gotoandplay.smartfoxserver.*;
	import it.gotoandplay.smartfoxserver.data.Room;
	import it.gotoandplay.smartfoxserver.data.User;
	
	//-----------------------------------------------------------------------------------//
	[Frame(factoryClass="Preloader")]
	[SWF(width="600", height="400", frameRate="60", backgroundColor="#7F7F7F")]	
	public class MekanetClient extends Sprite
	{
		private var sfc:SmartFoxClient;
		private var testObject:Object = {};
		private var grid:Grid;
		private var userName:String;
		
		//multidimensional array
		private var userList:Array;
		private var userListTextField:TextField;
		
		private var fbook:Facebook;
		private var session:FacebookSessionUtil;	
		
		
		//-------------------------------------------------------------------------------//
		public function MekanetClient()
		{
			grid = new Grid();
						
			userList = new Array();
			userListTextField = new TextField();
			userListTextField.background = true;
			userListTextField.backgroundColor = 0xFF00FF;
			
			
			this.addChild(grid);
			this.addChild(userListTextField);
			
		}
		//-------------------------------------------------------------------------------//
		public function Init():void
		{
			
			sfc = new SmartFoxClient(false);
			sfc.addEventListener(SFSEvent.onConnection,OnConnection);
			sfc.addEventListener(SFSEvent.onExtensionResponse,OnExtensionResponse);
			sfc.addEventListener(SFSEvent.onLogin,OnLogin);
			sfc.addEventListener(SFSEvent.onJoinRoom,OnJoinRoom);
			sfc.addEventListener(SFSEvent.onRoomLeft,OnRoomLeft);
			sfc.addEventListener(SFSEvent.onJoinRoomError,OnJoinRoomError);
			sfc.addEventListener(SFSEvent.onRoomListUpdate,onRoomListUpdate);
			
			sfc.addEventListener(SFSEvent.onUserEnterRoom,OnUserEnterRoom);
			sfc.addEventListener(SFSEvent.onUserLeaveRoom,OnUserLeaveRoom);
			
						
			
			//inicializo el login de facebook
			//fbook = new Facebook();
			session = new FacebookSessionUtil("871863bb8d53fafa205a68506a1a9643",
			"4bdf2698e88fe967b5ecb11ac60cd8f3",loaderInfo);
			fbook = session.facebook;
			session.login();
			
			
			//Me conecto al smartfox
			
			//sfc.connect("danielbrenner.com.ar");
			sfc.connect("localhost");
		}
		//-------------------------------------------------------------------------------//
		public function OnConnection(ev:SFSEvent):void
		{
			if (ev.params.success)
			{
				trace ("Me conecte a IP "+sfc.ipAddress+
				" Puerto:"+sfc.port);
				
				sfc.login("sampleZone","","");
				//sfc.login("simpleChat","","");
				
			}
			else 
			{
				trace("Error in the connection");
			}
		}
		//-------------------------------------------------------------------------------//
		public function OnLogin(sfc:SFSEvent):void
		{
			
			if (sfc.params.success)
			{
				userName = sfc.params.name;
				trace ("Welcome "+userName);
			}
			else
			{
				trace ("Error logging in: "+sfc.params.error);
			}
		}
		//-------------------------------------------------------------------------------//
		public function OnJoinRoom(ev:SFSEvent):void
		{
			var roomJoined:Room = ev.params.room;
			var roomUserList:Array = roomJoined.getUserList();
			
			trace ("room: "+ roomJoined.getName() + " joined");
					
			//limpio el textfield de lista en el cuarto
			userList.text = "";
			for each (var user:User in roomUserList)
				userList.push(user.getName());
			userList.sort();
			
			for each (var name:String in userList)
				userListTextField.appendText(name+"\n");
						
				
			//testObject.values = [100,200,300,400,500];
			//sfc.sendXtMessage("myExt","double",testObject,"xml");
		}
		//-------------------------------------------------------------------------------//
		public function OnRoomLeft(ev:SFSEvent):void
		{
			
			
		}
		//-------------------------------------------------------------------------------//
		public function OnJoinRoomError(ev:Event):void
		{
			trace ("error joining room");
		}
		//-------------------------------------------------------------------------------//
		public function OnUserEnterRoom(ev:SFSEvent):void
		{
			var user:User = ev.params.user;
			this.userListTextField.text = "";
			userList.push(user.getName());
			userList.sort();
					
			for each (var name:String in userList)
				userListTextField.appendText(name+"\n");
			//serListTextField.appendText(user.getName()+"\n");
			
			
			
		}
		//-------------------------------------------------------------------------------//
		public function OnUserLeaveRoom(ev:SFSEvent):void
		{
			userList.splice(userList.indexOf(ev.params.userName));
			userList.sort();
			this.userListTextField.text = "";
			for each (var name:String in userList)
				userListTextField.appendText(name+"\n");
				
			
		
		}
		//-------------------------------------------------------------------------------//
		public function onRoomListUpdate(ev:Event):void
		{
			trace ("room list updated");
			sfc.autoJoin();
		}
		//-------------------------------------------------------------------------------//
		public function OnExtensionResponse(obj:Object):void
		{
			trace ("me respondio");
			
			var dataObj:Object = obj.params.dataObj;
			var cmd:String = dataObj._cmd;
			
			//aca manejo la respuesta
			var val:Object;
			if (cmd == "double")
			{
				for each (val in dataObj.values)
					trace(val);
				
				sfc.sendXtMessage("myExt","sayHola",testObject);
			}
			if (cmd == "sayHola")
			{
				trace (dataObj.values);
			}
		}
		//-------------------------------------------------------------------------------//
		public function Update(ev:Event):void
		{
			//trace ("Update");
			
		}
		//-------------------------------------------------------------------------------//
	}
	//-----------------------------------------------------------------------------------//
}
//---------------------------------------------------------------------------------------//
